<?php
declare (strict_types = 1);

namespace app\api\middleware;

use app\api\service\User as ServiceUser;
use app\api\validate\User as ValidateUser;
use app\lib\exception\Miss;

class User
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
          $params=$request->param();
          $action=$request->action();
          
          $scene=validate(ValidateUser::class)->scene($action);
          $scene->check($params);
          $params=$scene->getDateByRule($params);
        
           $exit=(new ServiceUser())->checkuser($params['username'],$params['password']);
          
           if(!$exit)
           {
              return fail('用户不存在');
           }
          else{

             $token = $request->buildToken('__token__', 'sha1');
             $params['msg']=['uid'=>$exit->id,'token'=>$token];
          }
        $request->params=$params;
        return $next($request);
    }
    
}
